clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_raw"
global Output "Y:/limited/Michigan_CTE/funding_change/data_derived"


// LOAD DATA
local vars ric year grade cipcode adv_p4 total_segments segment13 total_mins assess* psn fano cepd
use `vars' using ${Input}/cte_program_enrollment.dta, clear

* keep students in sample
rename ric student
merge m:1 student using "Y:/limited/Michigan_CTE/funding_change/data_final/compiled_student_analysis_data.dta",  ///
	keep(3) keepusing(cohort9) nogen
	
drop if year > (cohort9 + 3)  	
drop if year < (cohort9)  		
	
	
*Cooking gets relabeled
replace cipcode = "12.0500" if cipcode == "12.9999"

* Family and consumer science is not federally recognized and is new in 2019
drop if cipcode=="19.0000"
	
//Segments
replace total_segments = 13 if total_segments==12 & inrange(segment13,2,4)
bys student year psn (total_segments): keep if _n==_N


//Courses
merge 1:m student year psn using ${Output}/student_course_data.dta, keep(1 3) nogen

preserve

 
bys student psn (year course_begin): gen cum_course = _n
replace cum_course = 4 if cum_course>4
bys student psn year (course_begin):keep if _n==_N
gen all = 1
gcollapse (min) mi = total_segments (p10) p10=total_segments (p25)  p25=total_segments (p50) p50=total_segments ///
			(p75) p75=total_segments (p90) p90=total_segments (max)  ma=total_segments (sum) all, by(psn year cum_course cipcode cepd)

bys psn: gegen min_year = min(year)
bys psn: gegen max_year = max(year)

drop if max_year<=2009| min_year>=2019
			
bys psn cum_course: gegen placebo_seg = median(cond(inrange(year,2009,2011),p50,.))
bys psn cum_course: gegen pre_seg = median(cond(inrange(year,2012,2014),p50,.))
bys psn cum_course: gegen post_seg = median(cond(inrange(year,2017,2019),p50,.))

keep if year ==2014

gen change_seg = post_seg - pre_seg
gen placebo_change_seg =  pre_seg - placebo_seg

tw (hist change [fw=all], color(blue%50) w(1) start(-12.5)) (hist placeo_change [fw=all], color(red%50)  w(1) start(-12.5)) ///
	if all>30, legend(order(1 "Difference Around Funding Change" 2 "Placebo Difference") row(2)) xlab(-12(4)12) xsize(8) ///
	xtitle(" " "Difference in Segments Conditional on Number of Courses in PSN")

	

tw (hist change [fw=all], color(blue%50) w(1) start(-12.5)) (hist placeo_change [fw=all], color(red%50)  w(1) start(-12.5)) ///
	if cum_c==1, legend(order(1 "Difference Around Funding Change" 2 "Placebo Difference") row(2)) xlab(-12(4)12) xsize(8) ///
	xtitle(" " "Difference in Segments Conditional on Number of Courses in PSN")

tw (hist change [fw=all], color(blue%50) w(1) start(-12.5)) (hist placeo_change [fw=all], color(red%50)  w(1) start(-12.5)) ///
	if cum_c==2, legend(order(1 "Difference Around Funding Change" 2 "Placebo Difference") row(2)) xlab(-12(4)12) xsize(8) ///
	xtitle(" " "Difference in Segments Conditional on Number of Courses in PSN")

tw (hist change [fw=all], color(blue%50) w(1) start(-12.5)) (hist placeo_change [fw=all], color(red%50)  w(1) start(-12.5)) ///
	if cum_c==3, legend(order(1 "Difference Around Funding Change" 2 "Placebo Difference") row(2)) xlab(-12(4)12) xsize(8) ///
	xtitle(" " "Difference in Segments Conditional on Number of Courses in PSN")

tw (hist change [fw=all], color(blue%50) w(1) start(-12.5)) (hist placeo_change [fw=all], color(red%50)  w(1) start(-12.5)) ///
	if cum_c==4, legend(order(1 "Difference Around Funding Change" 2 "Placebo Difference") row(2)) xlab(-12(4)12) xsize(8) ///
	xtitle(" " "Difference in Segments Conditional on Number of Courses in PSN")

preserve
gen all_2 = 1	
gcollapse (p50) change_seg placebo_change_seg (sum) all_2 [ fw=all], by(psn)

tw (hist change [fw=all], color(blue%50) w(1) start(-12.5)) (hist placebo_change_seg [fw=all], color(red%50)  w(1) start(-12.5)) ///
	if all>30, legend(order(1 "Difference Around Funding Change" 2 "Placebo Difference") row(2)) xlab(-12(4)12) xsize(8) ///
	xtitle(" " "Difference in Segments Conditional on Number of Courses in PSN")

	








***
* Label
***
drop all

label var year 			    "9th Grade Cohort Year"
label var cipcode 
label var dcode 			"District (or ISD)"
label var psn_segments 		"Average Number of Segments in PSN"
label var psn_course_count 	"Average Number of Courses taken in PSN"

label var any 				"Number of Students in PSN"
label var comp				"Number of Completers in PSN" 
label var segQ				"Number of Segment Q Completers in PSN" 
label var comp_course 		"Number of Students in PSN"
label var part_rate			"Unconditional Participation Rate in PSN" 
label var comp_rate			"Unconditional Complietion in PSN"
label var  Q_rate 			"Unconditional Segment Q Rate  in PSN"

label var pct_nonwhite 		"Percent Nonwhite in District (or ISD)"
label var pct_econdis  		"Percent Poor in District (or ISD)"
label var av_8attend  		"Average 8th Grade Attendance in District (or ISD)"
label var av_8math 	 		"Average 8th Grade Math Scores in District (or ISD)"
label var av_8read 	 		"Average 8th Grade Reading Scores in District (or ISD)"

label var pct_cte_any  		"Percent Any CTE in District (or ISD)"
label var pct_cte_comp  	"Percent Complete CTE in District (or ISD)"
label var cte_millage_pp  	"Per Pupil Millage Revenue in District (Average in ISD)"
label var hs_students  		"Total Students in District (Average in ISD)"
label var d_urbanicity 		"Urbanicity of District (Most Urban in ISD)"


*save "Y:/limited/Michigan_CTE/funding_change/data_final/psn_analysis.dta", replace
			
			



			
			